const { blog, user } = require('./model');

!(async () => {
    // 查询一条记录
    const bigFinish = await user.findOne({
        where: {
            username: '鱼儿',
        }
    });

    // 查询特定的列
    const bigFinishCopy = await user.findOne({
        attributes: ['username', 'nickname'],
        where: {
            username: '鱼儿',
        }
    }); // { username: '鱼儿', nickname: '飞鱼'}

    // 查询一个列表(所有)
    const all = await blog.findAll({
        where: {
            userId: 1,
        },
        order: [
            ['id', 'desc'], // id: 按id排序  desc: 倒序
        ]
    });

    // 分页查询
    const page = blog.findAll({
        limit: 2, // 查询条数
        offset: 0, // 跳过多少条
        order: [
            ['id', 'desc'],
        ]
    });

    // 查询总数
    const totalNum = await blog.findAndCountAll({
        limit: 2,
        offset: 0,
        order: [
            ['id', 'desc'],
        ]
    }); // totalNum.count 所有的总数,不考虑分页     totalNum.rows 查询到的记录

    // 连表查询
    const userListWithBlog = await user.findAndCountAll({
        attributes: ['username', 'nickname'],
        include: [
            {
                model: blog,
            }
        ]
    });

    console.log(JSON.stringify(userListWithBlog))
})()